(function (angular) {
    'use strict';

    angular.module('ytong.services').factory('feedbacksManager', feedbacksManager);

    function feedbacksManager(proxyService, $log, Constants, growl, authenticationService){

        var OPEN = "0";
        var CLOSED = "1";
        var REVIEWING = "2";

        var NOT_READ = "0";

        var feedbacks = [];
        var messages = [];
        var delayed_feedbacks = [];

        function loadFeedbacks(){

            proxyService.get(Constants.URLS.FEEDBACK_URL)
                .then(function(responseData){
                    $log.info("Feedbacks retrieved successfully.");
                    feedbacks = responseData.feedbacks;
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        feedbacks = [];
                        $log.info("No feedbacks.");
                    }else {
                        $log.info("Failed retrieving feedbacks  from server: " + responseData.log_error);
                        growl.error("Failed retrieving feedbacks");
                        feedbacks = [];
                    }
                });
        };

        function loadDelayedFeedbacks(){
            proxyService.get(Constants.URLS.FEEDBACKS_REPORT_URL)
                .then(function(responseData){
                    $log.info("Delayed feedbacks retrieved successfully.");
                    delayed_feedbacks = responseData.feedbacks_report.delayed_feedbacks;
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        delayed_feedbacks = [];
                        $log.info("No delayed feedbacks.");
                    }else {
                        $log.info("Failed retrieving delayed feedbacks  from server: " + responseData.log_error);
                        growl.error("Failed retrieving delayed feedbacks");
                        delayed_feedbacks = [];
                    }
                });
        };

        function loadMessages(){
            var userId = authenticationService.getCurrentUserID();

            proxyService.get(Constants.URLS.FEEDBACK_URL, {params: {id: userId}})
                .then(function(responseData){
                    $log.info("Messages retrieved successfully.");
                    messages = responseData.feedbacks;
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        messages = [];
                        $log.info("No messages.");
                    }else {
                        $log.info("Failed retrieving messages  from server: " + responseData.log_error);
                        growl.error("Failed retrieving messages");
                        messages = [];
                    }
                });
        };

        function getMessages(){
            return messages;
        };

        function getUnreadMeesagesCount(){
            if(messages === undefined){
                return 0;
            }

            var count = 0;
            for(var i=0; i< messages.length; i++){
                if(messages[i].response_read === NOT_READ){
                    count++;
                }
            }
            return count;
        };

        function getFeedbacks(){
            return feedbacks;
        }

        function getOpenFeedbacksCount(){
            if(feedbacks === undefined){
                return 0;
            }

            var count = 0;
            for(var i=0; i< feedbacks.length; i++){
                if((feedbacks[i].status === OPEN) || (feedbacks[i].status === REVIEWING)){
                    count++;
                }
            }
            return count;
        }

        function addFeedback(feedbackData){
            var userId = authenticationService.getCurrentUserID();

            var feedback = {};
            feedbackData.user = userId;
            feedback.feedback = feedbackData;

            proxyService.addEntity(Constants.URLS.FEEDBACK_URL,feedback)
                .then(function(){
                    growl.success("Feedback added");
                    loadFeedbacks();
                }, function(){
                    growl.error("Feedback adding failed");
                });
        };

        function updateFeedback(feedbackData, isMessage){

            var entity = "Feedback";
            if(isMessage){
                entity = "Message";
            }

            var feedback = {};
            feedback.feedback = feedbackData;

            proxyService.updateEntity(Constants.URLS.FEEDBACK_URL,feedback)
                .then(function(){
                    growl.success(entity + " updated");
                    loadFeedbacks();
                }, function() {
                    growl.error(entity + " updating failed");
                });
        };

        function getMessageStatus(message){
            switch(message.status){
                case OPEN: return "Open";
                case CLOSED: return "Closed";
                case REVIEWING: return "Reviewing";
            }
        };

        function getDelayedFeedbacks(){
            return delayed_feedbacks;
        };

        //interface
        return{
            loadFeedbacks: loadFeedbacks,
            getFeedbacks: getFeedbacks,
            getOpenFeedbacksCount: getOpenFeedbacksCount,
            addFeedback: addFeedback,
            updateFeedback: updateFeedback,
            loadMessages: loadMessages,
            getMessages: getMessages,
            getUnreadMeesagesCount: getUnreadMeesagesCount,
            getMessageStatus: getMessageStatus,
            loadDelayedFeedbacks: loadDelayedFeedbacks,
            getDealayedFeedbacks: getDelayedFeedbacks
        }

    };


}(angular));